package com.hanthink.web.controller.app.photoalbum;

import com.hanthink.common.annotation.Anonymous;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.utils.StringUtils;
import com.hanthink.system.domain.protocol.photoalbum.PhotoAlbumAddRequest;
import com.hanthink.system.domain.protocol.photoalbum.PhotoAlbumCancelRequest;
import com.hanthink.system.domain.protocol.photoalbum.PhotoAlbumPageRequest;
import com.hanthink.system.domain.protocol.photoalbum.PhotoAlbumPageResponse;
import com.hanthink.system.service.photoalbum.PhotoAlbumService;
import io.swagger.annotations.Api;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;


/**
 * 相册控制层
 *
 * @author WangBin
 * @date 2025年08月22日
 */
@RestController
@RequestMapping("/app/photo_album")
@Api(value = "app相册", tags = {"相册API接口"}, hidden = true)
public class PhotoAlbumController extends BaseController {

    @Autowired
    private PhotoAlbumService photoAlbumService;

    /**
     * 保存相册信息
     *
     * @param request
     * @return
     */
    @Anonymous
    @PostMapping("/save")
    public AjaxResult savePhotoAlbum(@Valid @RequestBody PhotoAlbumAddRequest request) {
        if (StringUtils.isEmpty(request.getUserId())) {
            return AjaxResult.error("用户登录超时，退出后再试!");
        }
        boolean result = photoAlbumService.savePhotoAlbum(request);
        if (!result) {
            return AjaxResult.error("保存相册失败！");
        }
        return AjaxResult.success();
    }

    /**
     * 获取相册列表数据
     *
     * @param request
     * @return
     */
    @Anonymous
    @PostMapping("/page")
    public TableDataInfo pagePhotoAlbum(@RequestBody PhotoAlbumPageRequest request) {
        startPage();
        if (StringUtils.isEmpty(request.getCreatorId())) {
            return getDataTable(Collections.emptyList());
        }
        List<PhotoAlbumPageResponse> list = photoAlbumService.getPhotoAlbumPage(request);
        return getDataTable(list);
    }

    /**
     * 删除相册信息
     *
     * @param request
     * @return
     */
    @Anonymous
    @PostMapping("/cancel")
    public AjaxResult cancelPhotoAlbum(@RequestBody PhotoAlbumCancelRequest request) {
        if (request.getIds() == null) {
            return AjaxResult.error("删除数据异常，退出后再试!");
        }
        boolean result = photoAlbumService.cancelPhotoAlbum(request.getIds());
        if (!result) {
            return AjaxResult.error("删除相册数据失败！");
        }
        return AjaxResult.success();
    }
}
